<template>
  <div class="change-container">
    <!-- 顶部金额卡片 -->
    <div class="amount-card">
      <div class="amount-item">
        <div class="flex items-center justify-center mb-[10px]">
          <img :src="jb" class="w-[20px] h-[20px]">
          <div class="label">{{ t('change.myCoins') }}</div>
        </div>
        <div class="number">{{ data.user_coins }}</div>
        <div class="rmb">≈{{data.estimated_currency}}{{ data.estimated_cash }}</div>
      </div>
      <div class="amount-item">
        <div class="flex items-center justify-center mb-[10px]">
          <img :src="jb" class="w-[20px] h-[20px]">
          <div class="label">{{ t('change.exchanged') }}</div>
        </div>
        <div class="number">{{data.total_trade_currency}} {{ data.total_trade_amount }}</div>
        <div class="record-btn" @click="showRecord">{{ t('change.exchangeRecord') }}</div>
      </div>
    </div>

    <!-- Touch Go 兑换选项 -->
    <div class="exchange-section" v-for="(item,index) in goodsListObj">
      <div class="section-title">{{index}}</div>
      <div class="options-grid">
        <div 
          class="exchange-option" 
          v-for="(goods, index1) in item as any" 
          :key="index"
          @click="handleExchange(goods)"
        >
          <div class="option-icon">
            <!-- <img src="@/assets/image/Frame@2x.png" alt="Touch Go"> -->
            <img :src="goods.exchange_icon" alt="T">
          </div>
          <!-- <div class="option-name">{{index}}</div> -->
          <div class="option-amount">{{goods.exchange_actual_currency}} {{ goods.exchange_actual_amount }}</div>
          <div class="option-points" @click="handleExchange(item)">
            <img src="@/assets/image/c1.png" alt="">
            <div>
              -{{ goods.asset_amount }}
            </div>
          </div>
        </div>
        
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import api from "@/api/api";
import jb from "@/assets/image/jb.jpg";
import { showToast } from "vant";
import { useI18n } from "vue-i18n";
const { t, locale } = useI18n();
import { useAutoRequest } from "@/hooks/loading";
const router = useRouter()




// 方法定义
const showRecord = () => {
  router.push({
    path: '/exchange-record',
    query: {
      package_name: route.query.package_name,
      device_id: route.query.device_id
    }
  })
}

const handleExchange = (option) => {
  console.log('兑换选项:', option)
  // TODO: 实现兑换逻辑
  router.push({
    path: '/TouchGo',
    query: {
      package_name: route.query.package_name,
      device_id: route.query.device_id,
      ...option,
      user_filter: JSON.stringify(option.user_filter)
    }
  })
}


// 基本数据
const data = ref<any>({});
const [loading, getGameBalance] = useAutoRequest(api.getGameBalance);

const getData = async () => {
  const res = await getGameBalance({
    // 从路由参数中获取 package_name 和 device_id
    package_name: route.query.package_name,
    device_id: route.query.device_id
  });
  data.value = res;
  if (data.value?.user_status === 'disable') {
    router.back()
  }
};


// 商品列表
const [loading1, gameExchange] = useAutoRequest(api.gameExchange);
const goodsListObj = ref({})
const getGoods = async()=> {
  const data = await gameExchange({
    package_name: route.query.package_name,
    device_id: route.query.device_id
  })
  goodsListObj.value = {
    ...data
  }
  console.log(data);
}


// 监听是否有参数
const route = useRoute()
watchEffect(() => {
  const package_name = route.query.package_name
  const device_id = route.query.device_id
  if (device_id && package_name) {
    getData();
    getGoods();
  }
});
</script>

<style scoped lang="scss">
.change-container {
  padding: 15px 10px;
  min-height: 100vh;
  background: #F6F6F6;
}

.amount-card {
  display: flex;
  background: linear-gradient( 180deg, #FF7A5C 0%, #FFB967 100%);
  border-radius: 16px;
  padding: 10px;
  color: white;
  margin-bottom: 20px;
  
  .amount-item {
    flex: 1;
    text-align: center;
    
    .label {
      font-size: 16px;
    }
    
    .number {
      font-size: 20px;
      font-weight: bold;
      margin-bottom: 12px;
    }
    
    .rmb, .record-btn {
      background: white;
      border-radius: 20px;
      padding: 8px 16px;
      display: inline-block;
      font-size: 14px;
      font-weight: bold;
    }

    .rmb {

      color: #FF3131;
    }

    .record-btn {
      color: #4caf50;
    }
  }
}

.exchange-section {
  background: white;
  border-radius: 12px;
  padding: 10px;
  margin-bottom: 16px;
  
  .section-title {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 16px;
  }
  
  .options-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
  }
  
  .exchange-option {
    background: #fff5f2;
    border-radius: 12px;
    padding: 8px;
    text-align: center;
    cursor: pointer;
    
    .option-icon {
      display: flex;
      justify-content: center;
      img {
        width: 60px;
        height: 30px;
      }
    }
    
    .option-name {
      font-size: 16px;
      font-weight: bold;
    }
    
    .option-amount {
      font-size: 16px;
      margin: 5px 0 ;
    }
    
    .option-points {
      font-size: 12px;
      margin: 5px 0;
      color: #fff;
      background: #24CE35;
      border-radius: 39px;
      height: 24px;
      display: flex;
      align-items: center;
      justify-content: center;
      img {
        width: 20px;
        height: 16px;
      }
    }
  }
}
</style>
    